package cn.b.sky.mes.qm.service.impl;

import java.util.List;

import cn.b.sky.mes.qm.model.DefectType;
import cn.b.sky.mes.qm.model.condition.DefectTypeCondition;
import cn.b.sky.mes.qm.service.DefectTypeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.qm.dao.DefectTypeDao;

@Service
public class DefectTypeServiceImpl implements DefectTypeService {

  private static final Logger LOG = LoggerFactory.getLogger(DefectTypeServiceImpl.class);

  private static final String DEFAULT_ORDER_FIELD = "CreateDateTime";

  @Autowired
  private DefectTypeDao defectTypeDao;

  @Override
  public List<DefectType> selectDefectTypeListByCondition(DefectTypeCondition condition) {
    if (null == condition) {
      condition = new DefectTypeCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField(DEFAULT_ORDER_FIELD);
    }
    return defectTypeDao.selectDefectTypeListByCondition(condition);
  }

  @Override
  public int selectDefectTypeCountByCondition(DefectTypeCondition condition) {
    if (null == condition) {
      condition = new DefectTypeCondition();
    }
    return defectTypeDao.selectDefectTypeCountByCondition(condition);
  }

  @Override
  public DefectType getDefectType(String siteCode, String defectTypeCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectTypeCode)) {
      return null;
    }
    return defectTypeDao.getDefectType(siteCode, defectTypeCode);
  }

  @Override
  public void insertDefectType(DefectType defectType) {
    if (StringUtils.isBlank(defectType.getSiteCode()) || StringUtils.isBlank(defectType.getDefectTypeCode())) {
      return;
    }
    defectTypeDao.insertDefectType(defectType);
  }

  @Override
  public void updateDefectType(DefectType defectType) {
    if (StringUtils.isBlank(defectType.getSiteCode()) || StringUtils.isBlank(defectType.getDefectTypeCode())) {
      return;
    }
    defectTypeDao.updateDefectType(defectType);
  }

  @Override
  public BatchJobReport deleteDefectType(String siteCode, String defectTypeCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectTypeCode)) {
      return new BatchJobReport("", "siteCode or defectTypeCode is null", false);
    }
    String message = defectTypeCode;
    boolean success = true;
    String cause = "";

    try {
      defectTypeDao.deleteDefectType(siteCode, defectTypeCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(errorMsg)) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(), languageCode,
            "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(), languageCode,
                "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success);
  }

  @Override
  public List<DefectType> selectDefectTypeForSuggest(DefectTypeCondition condition) {
    return defectTypeDao.selectDefectTypeForSuggest(condition);
  }

}
